<template>
  <el-dialog v-model="visible" title="新增二级指标" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="父ID" prop="parentId">
        <el-input v-model="dataForm.parentId" placeholder="父ID" readonly></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="standardName">
        <el-input v-model="dataForm.standardName" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="standardRemark">
        <el-input v-model="dataForm.standardRemark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="总分" prop="totalScore">
        <el-input v-model="dataForm.totalScore" placeholder="总分"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";

const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  parentId: 0,
  standardType: "",
  standardName: "",
  standardRemark: "",
  sort: "",
  creator: "",
  createDate: "",
  updater: "",
  updateDate: "",
  isDeleted: "",
  totalScore: ""
}) as any;

const rules = ref({});

const init = (id?: number) => {
  visible.value = true;
  dataForm.parentId = id;
  debugger;
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/point/pointstandard", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
